#include "PlayerStateIdle.h"
#include "Player.h"

PlayerStateIdle::PlayerStateIdle(Player* player) : 
PlayerState(player),
_idleState(0),
_blinkState(0)
{}

PlayerStateIdle::~PlayerStateIdle(void){}

void PlayerStateIdle::activate()
{
	_player->getAnimationMgr().setAnimation("idle");
	_idleState = 0;
	_blinkState = 0;
}

void PlayerStateIdle::deactivate()
{

}

int PlayerStateIdle::update()
{
	if (_player->damageReceived())
		return PSRV_TAKE_DAMAGE;

	if ((_player->flags() & LADDER_BELOW) == LADDER_BELOW)
	{
		_player->removeFlag(LADDER_BELOW);
		return PSRV_CLIMB_DOWN;
	}

		if (_idleState == 0)
		{
			if (_player->getAnimationMgr().getCurrentID() == "idle")
			{
				if (_player->getAnimationMgr().isDone())
				{
					_idleState = 1;
					if (_blinkState == 0)
						_player->getAnimationMgr().setAnimation("idle_blink_slow");
					else if (_blinkState == 1)
						_player->getAnimationMgr().setAnimation("idle_blink_fast");
				}
			}
		}
		else
		{
			if (_blinkState == 0)
			{
				if (_player->getAnimationMgr().getCurrentID() == "idle_blink_slow")
				{
					if (_player->getAnimationMgr().isDone())
					{
						_idleState = 0;
						_blinkState = 1;
						_player->getAnimationMgr().setAnimation("idle");
					}
				}
			}
			else
			{
				
				if (_player->getAnimationMgr().getCurrentID() == "idle_blink_fast")
				{
					if (_player->getAnimationMgr().isDone())
					{
						_idleState = 0;
						_blinkState = 0;
						_player->getAnimationMgr().setAnimation("idle");
					}
				}
			}
		}

		if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_DOWN, 0), 0))
		{
			_player->sendLadderCheckBelow();
		}

		if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_JUMP, 0), 0))
		{
			_player->_jumpStartPosition = _player->_position.y;
			return PSRV_JUMPING;
		}
		else if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_SHOOT, 0), 0))
		{
			return PSRV_ATTACK;
		}
		else if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_DASH, 0), 0))
		{
			return PSRV_DASH;
		}
		else if (!_player->_onGround)
			return PSRV_FALLING;
		else if (_player->getMovementStateX() != MSX_NONE)
			return PSRV_MOVING_GROUND;

		return PSRV_NOTHING;
}